package ar.com.sebasoft.rembrandt;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import ar.com.sebasoft.rembrandt.util.SystemHelper;

import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;
import com.actionbarsherlock.view.MenuItem;

/**
 * An activity representing a single Gallery detail screen. This activity is
 * only used on handset devices. On tablet-size devices, item details are
 * presented side-by-side with a list of items in a {@link GalleryListActivity}.
 * <p>
 * This activity is mostly just a 'shell' activity containing nothing more than
 * a {@link GalleryDetailFragment}.
 */
public class GalleryDetailActivity extends SherlockFragmentActivity {

	GalleryDetailFragment mFragment;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.activity_gallery_detail);

		SystemHelper.setCurrentContext(this.getApplicationContext());

		// Show the Up button in the action bar.
		this.getSupportActionBar().setDisplayHomeAsUpEnabled(true);

		if (savedInstanceState == null) {
			Bundle arguments = new Bundle();
			arguments.putString(GalleryDetailFragment.ARG_ALBUM_ID, this.getIntent().getStringExtra(GalleryDetailFragment.ARG_ALBUM_ID));
			this.mFragment = new GalleryDetailFragment();
			this.mFragment.setArguments(arguments);
			this.getSupportFragmentManager().beginTransaction().add(R.id.gallery_detail_container, this.mFragment).commit();
		}
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
			case android.R.id.home:
				NavUtils.navigateUpTo(this, new Intent(this, GalleryListActivity.class));
				return true;
		}

		return super.onOptionsItemSelected(item);
	}

	@Override
	public void onLowMemory() {
		super.onLowMemory();
		if (this.mFragment != null) {
			this.mFragment.onLowMemory();
		}
	}

	@Override
	public void onBackPressed() {
		NavUtils.navigateUpTo(this, new Intent(this, GalleryListActivity.class));
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater infla = this.getSupportMenuInflater();
		infla.inflate(R.menu.menu_prefs, menu);
		return true;
	}
}
